﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using AutoMapper;
using FikriKibar.Common.Helpers;
using FikriKibar.DataAccess.Abstract;
using FikriKibar.DataAccess.ContextBase;
using FikriKibar.DataAccess.RepositoryBase;
using FikriKibar.Entities.Concrete;

namespace FikriKibar.DataAccess
{
    public class EfUserGroupRoleLineDal : EfEntityRepositoryBase<UserGroupRoleLine, FikriKibarDbContext>, IUserGroupRoleLineDal
    {
        protected override UserGroupRoleLine AddEntity(UserGroupRoleLine entity, FikriKibarDbContext context)
        {
            return context.UserGroupRoleLines.Add(entity);
        }

        protected override UserGroupRoleLine UpdateEntity(UserGroupRoleLine entity, FikriKibarDbContext context)
        {
            var oldEntity = GetEntity(entity, context);
            var newEntity = Mapper.Map(entity, oldEntity);
            context.Entry(oldEntity).CurrentValues.SetValues(newEntity);
            return newEntity;
        }

        protected override List<UserGroupRoleLine> GetEntities(FikriKibarDbContext context)
        {
            return context.UserGroupRoleLines
                .Include(e=>e.User_CreatedBy)
                .Include(e=>e.User_UpdatedBy)
                .Include(e=>e.UserGroup)
                .Include(e=>e.Role)
                .ToList();
        }

        protected override List<UserGroupRoleLine> FilterEntities(FikriKibarDbContext context, Expression<Func<UserGroupRoleLine, bool>> where)
        {
            return context.UserGroupRoleLines
                .Include(e=>e.User_CreatedBy)
                .Include(e=>e.User_UpdatedBy)
                .Include(e=>e.UserGroup)
                .Include(e=>e.Role)
                .Where(where)
                .ToList();
        }

        protected override List<UserGroupRoleLine> FilterEntities(FikriKibarDbContext context, Expression<Func<UserGroupRoleLine, bool>> where, SortHelper<UserGroupRoleLine> sortHelper)
        {
            var query = (IOrderedQueryable<UserGroupRoleLine>)context.UserGroupRoleLines
                .Include(e=>e.User_CreatedBy)
                .Include(e=>e.User_UpdatedBy)
                .Include(e=>e.UserGroup)
                .Include(e=>e.Role)
                .Where(where);

            query = sortHelper.GenerateOrderedQuery(query);
            return query.ToList();
        }

        protected override List<UserGroupRoleLine> FilterEntities(FikriKibarDbContext context, Expression<Func<UserGroupRoleLine, bool>> where, SortHelper<UserGroupRoleLine> sortHelper, int index, int size)
        {
            var query = (IOrderedQueryable<UserGroupRoleLine>)context.UserGroupRoleLines
                .Include(e=>e.User_CreatedBy)
                .Include(e=>e.User_UpdatedBy)
                .Include(e=>e.UserGroup)
                .Include(e=>e.Role)
                .Where(where);

            query = sortHelper.GenerateOrderedQuery(query);
            return query.Skip((index - 1) * size).Take(size).ToList();
        }

        protected override UserGroupRoleLine GetEntity(UserGroupRoleLine entity, FikriKibarDbContext context)
        {
            return context.UserGroupRoleLines
                .Include(e=>e.User_CreatedBy)
                .Include(e=>e.User_UpdatedBy)
                .Include(e=>e.UserGroup)
                .Include(e=>e.Role)
                .FirstOrDefault(e => e.Id == entity.Id);
        }

        public int GetTotalCount(Expression<Func<UserGroupRoleLine, bool>> where)
        {
            using (var context = new FikriKibarDbContext())
            {
                return context.UserGroupRoleLines.Where(where).Count();
            }
        }
    }
}

